package com.example.animationmodule

import android.media.Image
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.BounceInterpolator
import android.widget.Button
import android.widget.ImageView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import java.time.Instant
import java.util.*

private const val TAG = "InterpolatorActivity"
class InterpolatorActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var start: Button
    private lateinit var stop: Button
    private lateinit var imageView: ImageView
    private lateinit var loadAnimation:Animation

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_interpolator)
        this.start = findViewById(R.id.btn_inter_start)
        this.stop = findViewById(R.id.btn_inter_stop)
        this.imageView = findViewById(R.id.iv_interpolator)
        this.start.setOnClickListener(this)
        this.stop.setOnClickListener(this)
        Log.d(TAG, "onClick: ")
        this.loadAnimation=AnimationUtils.loadAnimation(this, R.anim.set_anim_inter)
        loadAnimation.interpolator = BounceInterpolator()
        loadAnimation.duration = 2000
        loadAnimation.setAnimationListener(object : Animation.AnimationListener {
            @RequiresApi(Build.VERSION_CODES.O)
            override fun onAnimationStart(animation: Animation?) {
//                TODO("Not yet implemented")
                val date = Instant.now()


                Log.d(TAG, "onAnimationStart: ${date.toString()}")
            }

            @RequiresApi(Build.VERSION_CODES.O)
            override fun onAnimationEnd(animation: Animation?) {
//                TODO("Not yet implemented")
                val date = Instant.now()
                Log.d(TAG, "onAnimationEnd:${date.toString()} ")
            }

            @RequiresApi(Build.VERSION_CODES.O)
            override fun onAnimationRepeat(animation: Animation?) {
//                TODO("Not yet implemented")
                val date = Instant.now()
                Log.d(TAG, "onAnimationRepeat: ${date.toString()}")
            }

        })
    }

    override fun onClick(v: View?) {
//        TODO("Not yet implemented")
        when (v?.id) {
            R.id.btn_inter_start -> {

                this.imageView.startAnimation(loadAnimation)

            }
            R.id.btn_inter_stop -> {
                this.loadAnimation.cancel()
            }
        }
    }
}
